package cn.iocoder.yudao.module.crm.controller.admin.module;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.module.vo.customerlimit.CrmModuleCustomerLimitRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.module.vo.customerlimit.CrmModuleCustomerLimitSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.module.CrmModuleCustomerLimitDO;
import cn.iocoder.yudao.module.crm.service.module.CrmModuleCustomerLimitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - CRM 客户限制配置")
@RestController
@RequestMapping("/crm/module-customer-limit")
@Validated
public class CrmModuleCustomerLimitController {

    @Resource
    private CrmModuleCustomerLimitService moduleCustomerLimitService;

    @PostMapping("/create")
    @Operation(summary = "创建客户限制配置")
    @PreAuthorize("@ss.hasPermission('crm:module:update')")
    public CommonResult<Long> createCustomerLimit(@Valid @RequestBody CrmModuleCustomerLimitSaveReqVO createReqVO) {
        return success(moduleCustomerLimitService.createCustomerLimit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户限制配置")
    @PreAuthorize("@ss.hasPermission('crm:module:update')")
    public CommonResult<Boolean> updateCustomerLimitConfig(@Valid @RequestBody CrmModuleCustomerLimitSaveReqVO updateReqVO) {
        moduleCustomerLimitService.updateCustomerLimit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户限制配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:module::delete')")
    public CommonResult<Boolean> deleteCustomerLimitConfig(@RequestParam("id") Long id) {
        moduleCustomerLimitService.deleteCustomerLimit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户限制配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CrmModuleCustomerLimitRespVO> getCustomerLimit(@RequestParam("moduleId") Long moduleId, @RequestParam("type") Integer type) {
        CrmModuleCustomerLimitDO customerLimit = moduleCustomerLimitService.getCustomerLimit(moduleId, type);
        return success(BeanUtils.toBean(customerLimit, CrmModuleCustomerLimitRespVO.class));
    }
}
